#include <iostream>    
#include <cstring>    
#include <vector>    
#include <unistd.h>    
#include <sys/types.h>    
#include <sys/wait.h>    
#include <functional>    
#include <cstdio>    
    
using namespace std;    
    
int main()    
{    
    
    pid_t id = fork();    
    if(id < 0)    
    {    
        printf("errno : %d, errstring: %s\n", errno, strerror(errno));    
        return errno;    
    }else if(id == 0)    
    {    
        int cnt = 5;    
        while(cnt--)    
        {    
            printf("子进程运行中, pid : %d\n", getpid());    
            sleep(1);    
        }    
    
        exit(123);    
    }else    
    {    
        sleep(6);    
    
        int status = 0;    
        pid_t rid = waitpid(id, &status, 0);      // 回收僵尸子进程    
        if(rid > 0)    
        {    
            if(WIFEXITED(status))
            {
                  printf("wait sub process success, rid : %d, status code: %d\n", rid, WEXITSTATUS(status));                                                                                                                       
                  // status是退出信息，退出码保存在status的前八位    
            }else{
                printf("child process quit error!\n");
            }
            
        }else{                                                                                                                                                                                                                                                            
            perror("waitpid");                                                                                                                                                                                                                                            
        }                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                          
        while(true)                                                                                                                                                                                                                                                       
        {                                                                                                                                                                                                                                                                 
            printf("我是父进程：pid%d\n", getpid());                                                                                                                                                                                                                      
            sleep(1);                                                                                                                                                                                                                                                     
        }                                                                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                          
    return 0;                                                                                                                                                                                                                                                             
}          
